package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Min;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer discontinued = setmealMapper.selectStatusCount(StatusConstant.DISABLE);
        Integer sold = setmealMapper.selectStatusCount(StatusConstant.ENABLE);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO overviewDishs() {
        Integer discontinued = dishMapper.selectStatusCount(StatusConstant.DISABLE);
        Integer sold = dishMapper.selectStatusCount(StatusConstant.ENABLE);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDateTime time = LocalDateTime.now().with(LocalTime.MIN);
        // 待接单
        Integer waitingOrders = orderMapper.selectBYTimeAndStatus(time, Orders.TO_BE_CONFIRMED);
        // 待派送
        Integer deliveredOrders = orderMapper.selectBYTimeAndStatus(time, Orders.CONFIRMED);
        // 已完成
        Integer completedOrders = orderMapper.selectBYTimeAndStatus(time, Orders.COMPLETED);
        // 已取消
        Integer cancelledOrders = orderMapper.selectBYTimeAndStatus(time, Orders.CANCELLED);
        // 所有
        Integer allOrders = orderMapper.selectBYTimeAndStatus(time, null);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
